<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>易买网 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .container {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 2rem;
            font-size: 2rem;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        label {
            display: block;
            margin-bottom: 0.5rem;
            color: #555;
            font-weight: bold;
        }
        
        input {
            width: 100%;
            padding: 12px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: transform 0.2s;
        }
        
        button:hover {
            transform: translateY(-2px);
        }
        
        .switch-form {
            text-align: center;
            margin-top: 1rem;
        }
        
        .switch-form a {
            color: #667eea;
            text-decoration: none;
            cursor: pointer;
        }
        
        .message {
            padding: 10px;
            margin: 1rem 0;
            border-radius: 5px;
            text-align: center;
        }
        
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        #registerForm {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>易买网</h1>
        
        <div id="message"></div>
        
        <!-- 登录表单 -->
        <form id="loginForm">
            <div class="form-group">
                <label for="loginName">用户名:</label>
                <input type="text" id="loginName" name="loginName" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <button type="submit">登录</button>
            
            <div class="switch-form">
                <span>没有账号？</span>
                <a onclick="showRegister()">立即注册</a>
            </div>
        </form>
        
        <!-- 注册表单 -->
        <form id="registerForm">
            <div class="form-group">
                <label for="regLoginName">用户名:</label>
                <input type="text" id="regLoginName" name="loginName" required>
            </div>
            
            <div class="form-group">
                <label for="regUserName">昵称:</label>
                <input type="text" id="regUserName" name="userName" required>
            </div>
            
            <div class="form-group">
                <label for="regPassword">密码:</label>
                <input type="password" id="regPassword" name="password" required>
            </div>
            
            <div class="form-group">
                <label for="sex">性别:</label>
                <select id="sex" name="sex" style="width: 100%; padding: 12px; border: 2px solid #ddd; border-radius: 5px;">
                    <option value="1">男</option>
                    <option value="2">女</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="mobile">手机号:</label>
                <input type="tel" id="mobile" name="mobile" required>
            </div>
            
            <div class="form-group">
                <label for="email">邮箱:</label>
                <input type="email" id="email" name="email" required>
            </div>
            
            <button type="submit">注册</button>
            
            <div class="switch-form">
                <span>已有账号？</span>
                <a onclick="showLogin()">立即登录</a>
            </div>
        </form>
    </div>

    <script>
        let token = '';
        
        function showMessage(message, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="message ${type}">${message}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 3000);
        }
        
        function showRegister() {
            document.getElementById('loginForm').style.display = 'none';
            document.getElementById('registerForm').style.display = 'block';
        }
        
        function showLogin() {
            document.getElementById('registerForm').style.display = 'none';
            document.getElementById('loginForm').style.display = 'block';
        }
        
        // 登录处理
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(e.target);
            const data = {
                loginName: formData.get('loginName'),
                password: formData.get('password')
            };
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    token = result.data.token;
                    localStorage.setItem('token', token);
                    localStorage.setItem('userInfo', JSON.stringify(result.data));
                    showMessage('登录成功！正在跳转...', 'success');
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 1000);
                } else {
                    showMessage(result.message, 'error');
                }
            } catch (error) {
                showMessage('网络错误，请重试', 'error');
            }
        });
        
        // 注册处理
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(e.target);
            const data = {
                loginName: formData.get('loginName'),
                userName: formData.get('userName'),
                password: formData.get('password'),
                sex: parseInt(formData.get('sex')),
                mobile: formData.get('mobile'),
                email: formData.get('email'),
                type: 0 // 普通用户
            };
            
            try {
                const response = await fetch('/api/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    token = result.data.token;
                    localStorage.setItem('token', token);
                    localStorage.setItem('userInfo', JSON.stringify(result.data));
                    showMessage('注册成功！正在跳转...', 'success');
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 1000);
                } else {
                    showMessage(result.message, 'error');
                }
            } catch (error) {
                showMessage('网络错误，请重试', 'error');
            }
        });
        
        // 检查是否已登录
        const savedToken = localStorage.getItem('token');
        if (savedToken) {
            // 验证token是否有效
            fetch('/api/auth/validate', {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + savedToken
                }
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 200 && result.data) {
                    window.location.href = '/';
                } else {
                    localStorage.removeItem('token');
                    localStorage.removeItem('userInfo');
                }
            })
            .catch(() => {
                localStorage.removeItem('token');
                localStorage.removeItem('userInfo');
            });
        }
    </script>
</body>
</html>